using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DB;

/// <summary>
/// Summary description for clsUser
/// </summary>
public class clsUser
{
    #region MEMBER VARIABLES

    private int m_iUserID;
    private String m_strFirstName;
    private String m_strSurname;
    private String m_strEmailAddress;
    private String m_strPassword;
    private Boolean m_bStaffMember;

    private clsUserSecurityList clsUserSecurityList;

    #endregion

    #region CONSTRUCTORS

    public clsUser()
    {
        //2007/10/03
        // This is really an invalid user. Without a valid username & password, it's impossible
        // to establish what rights may or may not be added to said user
        m_strEmailAddress = "";
        m_strPassword = "";
        Initialise();
    }

    public clsUser(String strEmailAddress, String strPassword)
    {
        //2007/10/03
        m_strEmailAddress = strEmailAddress;
        m_strPassword = strPassword;
        Initialise();
    }

    #endregion

    #region PROPERTIES

    public int iUserID
    {
        get
        {
            return m_iUserID;
        }
    }

    public string strFirstName
    {
        get
        {
            return m_strFirstName;
        }
    }

    public string strSurname
    {
        get
        {
            return m_strSurname;
        }
    }

    public string strEmailAddress
    {
        get
        {
            return m_strEmailAddress;
        }
    }


    public Boolean bStaffMember
    {
        get { return m_bStaffMember; }
        set { m_bStaffMember = value; }
    }


    #endregion

    #region PRIVATE METHODS

    private void Initialise()
    {
        try
        {
            //### This method checks whether the user is valid or not and assigns
            //### appropriate rights. If invalid, no rights are assigned.
            DataSet dtsUser = DataConnection.GetDataObject().GetData("Data", "spInitialiseUser '" + m_strEmailAddress + "','" + m_strPassword + "'");
            if (dtsUser.Tables[0].Rows.Count != 0)
            {
                //### A valid record was found - however, SQL syntax is not case sensitive
                //### Check the values again to ensure case sensitivity
                if (m_strEmailAddress.Equals(dtsUser.Tables["Data"].Rows[0]["strEmailAddress"].ToString()) && m_strPassword.Equals(dtsUser.Tables["Data"].Rows[0]["strPassword"].ToString()))
                {
                    //### Valid user - assign roles
                    m_iUserID = int.Parse(dtsUser.Tables["Data"].Rows[0]["iUserID"].ToString());
                    m_strFirstName = dtsUser.Tables["Data"].Rows[0]["strFirstName"].ToString();
                    m_strSurname = dtsUser.Tables["Data"].Rows[0]["strSurname"].ToString();
                    m_bStaffMember = Convert.ToBoolean(dtsUser.Tables["Data"].Rows[0]["bStaffMember"].ToString());
                }
                else
                {
                    throw new ApplicationException("Invalid username and / or password. Please try again.");
                }
            }
            else
            {
                throw new ApplicationException("Invalid username and / or password. Please try again.");
            }
            //*** clsUserSecurityList = new clsUserSecurityList(m_iUserID);   
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    #endregion

}
